/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import DAO.util.ConectaBanco;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.Login;


/**
 *
 * @author Alex
 */
public class LoginDAO {

    public boolean criar(Login login) throws SQLException {
      
            Connection conexao = ConectaBanco.getConexao();

            String sql = "insert into login(usuario, senha,adm,status) values (?,?,?,1)";
            // seta os parametros no sql
            PreparedStatement pstmt = conexao.prepareCall(sql);

            pstmt.setString(1, login.getUsuario());
            pstmt.setString(2, login.getSenha());
            pstmt.setBoolean(3, login.isAdm());

            System.out.println(sql);
            pstmt.execute();
            conexao.close();

            return true;




    }

    public ArrayList<Login> buscarTodas(String nome) throws ClassNotFoundException, SQLException {
        ArrayList<Login> logins = new ArrayList<Login>();
        Connection conexao = null;

            conexao = ConectaBanco.getConexao();
            String sql = "select * from login where usuario ilike ? and status = 1";

            PreparedStatement pst = conexao.prepareStatement(sql);

            pst.setString(1, nome + "%");
            ResultSet rs = pst.executeQuery();

            while (rs.next()) {
                Login login = new Login();
                login.setId(rs.getInt("id"));
                login.setUsuario(rs.getString("usuario"));
                login.setSenha(rs.getString("senha"));

                login.setAdm(rs.getBoolean("adm"));


                logins.add(login);
            }
            conexao.close();
            return logins;

      

    }

    public Login buscarLoginByCodigo(int codigo) throws ClassNotFoundException, SQLException {
        Connection conexao = ConectaBanco.getConexao();
        PreparedStatement sql = conexao.prepareStatement("select * from login where id=? ");
        sql.setInt(1, codigo);
        ResultSet resultado = sql.executeQuery();
        Login login = new Login();
        while (resultado.next()) {
            login.setId(resultado.getInt("id"));
            login.setUsuario(resultado.getString("usuario"));
            login.setSenha(resultado.getString("senha"));
            login.setAdm(resultado.getBoolean("adm"));
            login.setStatus(resultado.getInt("status"));

        }
        return login;
    }

    public boolean atualizar(Login login) throws SQLException {

            Connection conexao = ConectaBanco.getConexao();

            String sql = "update login set  senha=?,adm=? where id=?";

            PreparedStatement pstmt = conexao.prepareCall(sql);

            pstmt.setString(1, login.getSenha());
            pstmt.setBoolean(2,login.isAdm());
            pstmt.setInt(3, login.getId());

            System.out.println(sql);
            pstmt.execute();
   
            conexao.close();
            return true;
      
    }



    public boolean apagar(int id) throws SQLException {

      
            Connection conexao = ConectaBanco.getConexao();

            String sql = "delete from login where id = ?";
            PreparedStatement pstmt = conexao.prepareCall(sql);

            pstmt.setInt(1, id);

            pstmt.execute();

            conexao.close();
            return true;
      

    }

    public Login login(Login Xlogin) throws SQLException, Exception {
        Connection conexao = ConectaBanco.getConexao();
        String sql = "select * from login where usuario =? and senha=?";

        PreparedStatement pst = conexao.prepareStatement(sql);
        pst.setString(1, Xlogin.getUsuario());
        pst.setString(2, Xlogin.getSenha());
        Login login = new Login();

        ResultSet resultado = pst.executeQuery();
        System.out.println("login e senha " + Xlogin.getUsuario() + " " + Xlogin.getSenha());
        resultado.next();

        login.setId(resultado.getInt("id"));
        System.out.println("id!!!!!!!!!!!!" + login.getId());
        login.setUsuario(resultado.getString("usuario"));

        login.setSenha(resultado.getString("senha"));
        login.setAdm(resultado.getBoolean("adm"));
        login.setStatus(resultado.getInt("status"));

        conexao.close();
        return login;

    }

    public int idLogin(String userName, String senha) throws SQLException {
        Connection conexao = ConectaBanco.getConexao();
        PreparedStatement sql = conexao.prepareStatement("select id from login where username =? and senha=?");
        sql.setString(1, userName);
        sql.setString(2, senha);
        ResultSet resultado = sql.executeQuery();
        resultado.next();
        int id = resultado.getInt("id");

        return id;

    }



}
